package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 孙亚梅
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/9 18:01
 */
@Service
public class UserServiceImpl implements  UserService {

    @Autowired
    private UserMapper userMapper;

    //会报错, 无法注入, 因为request, response, sesson, model是springmvc的默认支持参数对象
    //是子容器中的对象, 而这里service属于spring父容器管理的对象, 无法使用子容器中的对象
    //@Autowired
    //private HttpServletRequest request;

    @Override
    public List<User> findUserAll() {
        //调用dao持久层
        List<User> list = userMapper.selectList(null);
        return list;
    }

    @Override
    public List<User> findByNameAndSexAndBirthday() {
        //1. 创建查询条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

        //2. 设置根据姓名模糊查询条件
        queryWrapper.like("name", "王");

        //3. 设置根据性别精确查询条件
        queryWrapper.eq("sex", "男");

        //4. 设置根据日期范围查询条件
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse("2021-02-01 00:00:00");
            queryWrapper.ge("birthday", date);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //5. 调用dao查询并返回结果
        List<User> list = userMapper.selectList(queryWrapper);
        //6. 返回
        return list;
    }
}
